package com.demo.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.annotation.StringRes;
import com.module.commonui.R;
import com.tg.appcommon.android.ResourcesUtil;
import com.tange.base.toolkit.StringUtils;

public class SettingItemTextView extends SettingItemLineView {
    private String mName;
    private String mContent;
    private TextView mTvName;
    private int mNameTextSize = 14;
    private TextView mTvContent;
    private int mTextColor;
    private TextView mTvNumber;
    private int mTextContentColor;
    private ImageView rightArrow;
    protected View mView;
    private int rightArrowMargin = 0;
    private boolean mShow = false;
    private boolean mShowDrawable = true;
    private boolean mShowRightArrow = true;
    private Drawable mDrawable;
    private Drawable mRightArrow;
    private float mRightArrowAlpha = 0.4f;
    private Drawable mContentDrawable;
    protected int resource;
    private int mContentType = 0;
    private int mNameType = 0;
    private TextView mTvUrl;
    private float mFloatWeightTextName, mFloatWeightContent;

    public SettingItemTextView(Context context) {
        super(context);
        initContentView(context);
    }

    public SettingItemTextView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SettingItemTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initParam(context, attrs);
        initContentView(context);
    }

    public int getLayoutResID() {
        return R.layout.settings_item_text;
    }

    private void initView(Context context){
        if (mView == null){
            mView = LayoutInflater.from(context).inflate(resource, this, true);
        }
    }


    private void initParam(Context context, @Nullable AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SettingItemTextView);
        resource = typedArray.getResourceId(R.styleable.SettingItemTextView_item_layout, getLayoutResID());
        mName = typedArray.getString(R.styleable.SettingItemTextView_text_name);
        mContent = typedArray.getString(R.styleable.SettingItemTextView_text_content);
        mShow = typedArray.getBoolean(R.styleable.SettingItemTextView_text_line_visibility, false);
        mDrawable = typedArray.getDrawable(R.styleable.SettingItemTextView_item_background);
        mRightArrow = typedArray.getDrawable(R.styleable.SettingItemTextView_item_right_arrow);
        rightArrowMargin = (int) typedArray.getDimension(R.styleable.SettingItemTextView_right_margin_arrow, 0);
        mContentDrawable = typedArray.getDrawable(R.styleable.SettingItemTextView_item_content_background);
        mShowDrawable = typedArray.getBoolean(R.styleable.SettingItemTextView_content_drawable_visibility, true);
        mFloatWeightTextName = typedArray.getFloat(R.styleable.SettingItemTextView_text_name_weight, 2.0f);
        mFloatWeightContent = typedArray.getFloat(R.styleable.SettingItemTextView_text_content_weight, 1.0f);
        mTextColor = typedArray.getColor(R.styleable.SettingItemTextView_text_name_color, context.getResources().getColor(R.color.global_customize_fixed_color_black));
        mTextContentColor = typedArray.getColor(R.styleable.SettingItemTextView_text_content_color,  context.getResources().getColor(R.color.global_customize_fixed_color_dark_gray));
        mContentType = typedArray.getInt(R.styleable.SettingItemTextView_text_content_style, 0);
        mNameType = typedArray.getInt(R.styleable.SettingItemTextView_text_name_style, 0);
        mShowRightArrow = typedArray.getBoolean(R.styleable.SettingItemTextView_right_arrow_visibility, true);
        mNameTextSize = typedArray.getInt(R.styleable.SettingItemTextView_text_name_size, mNameTextSize);
        mRightArrowAlpha = typedArray.getFloat(R.styleable.SettingItemTextView_item_right_alpha, mRightArrowAlpha);
        typedArray.recycle();
    }

    public void setNumber(String num){
        mTvNumber.setText(num);
        mTvNumber.setVisibility(StringUtils.isEmpty(num)? GONE : View.VISIBLE);
    }

    private void initContentView(Context context) {
        initView(context);
        setItemBackground();
        mTvName = mView.findViewById(R.id.tv_item_text_name);
        mTvContent = mView.findViewById(R.id.tv_item_text_content);
        mTvNumber = mView.findViewById(R.id.tv_item_text_number);
        rightArrow = mView.findViewById(R.id.iv_right_arrow);
        mTvUrl = mView.findViewById(R.id.tv_url);
        if (mContentDrawable != null){
            mTvContent.setBackground(mContentDrawable);
        }

        mTvName.setTextSize(TypedValue.COMPLEX_UNIT_SP, mNameTextSize);

        if (mNameType == 1){
            mTvName.setTypeface(null, Typeface.BOLD);
        }

        if (mContentType == 1){
            mTvContent.setTypeface(null, Typeface.BOLD);
        }

        rightArrow.setAlpha(mRightArrowAlpha);
        if (!mShowRightArrow){
            rightArrow.setVisibility(INVISIBLE);
            ViewGroup.LayoutParams layoutParams = rightArrow.getLayoutParams();
            layoutParams.width = 1;
            rightArrow.setLayoutParams(layoutParams);
        }else if (mRightArrow != null){
            rightArrow.setImageDrawable(mRightArrow);
        }

        if (rightArrowMargin != 0){
            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams)rightArrow.getLayoutParams();
            layoutParams.rightMargin = rightArrowMargin;
            rightArrow.setLayoutParams(layoutParams);
        }
        setContent(mTvName, mName, mTextColor);
        setContent(mTvContent, mContent, mTextContentColor);
        setTextCompoundDrawables();
        mLineView = mView.findViewById(R.id.rl_text_line);
        showLine(mShow);
        setWeight();
    }

    public void showNameDrawable(boolean show){
        if (mTvName != null){
            if (show){
                mTvName.setCompoundDrawablesWithIntrinsicBounds(null, null, ResourcesUtil.getDrawable(R.drawable.ic_tange_global_wiki), null);
            }else{
                mTvName.setCompoundDrawables(null, null, null, null);
            }
        }
    }

    private void setTextCompoundDrawables() {
        if (!mShowDrawable){
            mTvContent.setCompoundDrawables(null, null, null, null);
        }
    }

    private void setItemBackground() {
        if (mDrawable != null){
            setBackground(mDrawable);
            mView.findViewById(R.id.text_bg).setBackground(mDrawable);
        }
    }

    public void setBackgroundResource(int resid) {
        mView.findViewById(R.id.text_bg).setBackgroundResource(resid);
    }

    private void setContent(TextView tvContent, String content, int textContentColor) {
        tvContent.setText(content);
        tvContent.setTextColor(textContentColor);
    }

    public void showContent(boolean show){
        mTvContent.setVisibility(show? VISIBLE : GONE);
    }

    private void setWeight() {

    }

    public void setContent(String text){
        mContent = text;
        mTvContent.setText(text);
    }
    public void setName(@StringRes int resId){
        String name = ResourcesUtil.getString(resId);
        mTvName.setText(name);
    }

    public void setName(String name){
        mTvName.setText(name);
    }

    public void setNameColor(@ColorInt int color){
        mTvName.setTextColor(color);
    }

    public void setNumberColor(@ColorInt int color){
        mTvNumber.setTextColor(color);
    }
    public void setContent(@StringRes int resId){
        mContent = ResourcesUtil.getString(resId);
        mTvContent.setText(mContent);
    }

    public TextView getContent(){
        return mTvContent;
    }

    public TextView getUrlTextView(){
        return mTvUrl;
    }

    public void showUrlTextView(boolean show){
        if (mTvUrl != null){
            mTvUrl.setVisibility(show? VISIBLE : GONE);
        }
    }

    public void setRightArrow(int resId){
        rightArrow.setImageResource(resId);
    }

    public ImageView getRightArrow() {
         return rightArrow;
    }

    public String getName(){
        return mTvName.getText().toString();
    }

    public TextView getNameView(){
        return mTvName;
    }

    public void setContentTextColor(@ColorInt int color) {
        mTvContent.setTextColor(color);
    }

}
